const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const secToTime = time => {
  const data = parseInt(time)
  let m = parseInt(time / 60)
  let s = data % 60
  if (m < 10) {
    m = `0${m}`
  }
  if (s < 10) {
    s = `0${s}`
  }
  return `${m}:${s}`
}

//判断时间是否超过两个小时
const isOverdue = (time) => {
  const currentTime = new Date().getTime()
  return (currentTime - time) / 1000 / 60 / 60 < 2
}

// 数量为10000之后变为万
const overThousand = (item) => {
  if (item > 9999) {
    item = (item / 10000).toFixed(1)
    item = `${item}万`
  } else if (item <= 9999 && item > 1000) {
    item = (item / 1000).toFixed(1)
    item = `${item}千`
  }
  return item
}

// 成功弹窗
const showToast = (title) => {
  wx.showToast({
    title,
    icon: 'success',
    duration: 2000
  })
}
// 统计播放次数
const visitCount = (id, title) => {
  wx.reportAnalytics('visit_count', {
    id,
    title
  });
}
// 统计分享次数
const shareCount = (id, title) => {
  wx.reportAnalytics('share_count', {
    id,
    title
  });
}
// 统计生成相册次数
const saveCount = (id, title) => {
  wx.reportAnalytics('save_count', {
    id,
    title
  });
}
// 统计首页模板分享次数
const shareIndexCount = (id, title) => {
  wx.reportAnalytics('share_index_count', {
    id,
    title
  });
}
// 获取分享信息
const getShareData = () =>{
  const data = wx.getStorageSync('getShareData')
  let shareData = {
    title: '新年福到！这是我的全家福，您也来制作吧，保存幸福时刻！',
    imageUrl: 'https://mmbiz.qlogo.cn/mmbiz_png/m4fbm7nwuQcicibvx8jibnabjzkmjUDwiaz4lCiaOtLTUe7z95TuQ4mb2yiaXo3FkE9zVM54DDYlDpatV2CEdGcGnibXw/0'
  }
  if (data){
    if (data.shareData.title !== undefined && data.shareData.cover !== undefined){
      shareData = {
        title: data.shareData.title,
        imageUrl: data.shareData.imageUrl,
      }
    }
  }
  return shareData
}


module.exports = {
  isOverdue,
  formatTime,
  secToTime,
  overThousand,
  showToast,
  visitCount,
  shareCount,
  saveCount,
  shareIndexCount,
  getShareData
}
